package com.webinsky.article.persist.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import com.webinsky.article.persist.entity.ArticleRelationship;

public interface ArticleRelationshipMapper {
	@Delete(
			"delete from `relationship` where id = #{id, jdbcType=INTEGER}"
	)
	int deleteByArticleId(@Param("id")int id);	
	
	@Insert({
		"insert into `relationship` (site_id, type, cn_id, jp_id, en_id, kr_id, th_id, tw_id, create_at) values",
		"(#{siteId, jdbcType=INTEGER}, #{type, jdbcType=VARCHAR}, #{cnId, jdbcType=INTEGER}, #{jpId, jdbcType=INTEGER},  #{enId, jdbcType=INTEGER}, #{krId, jdbcType=INTEGER}, #{thId, jdbcType=INTEGER}, #{twId, jdbcType=INTEGER}, now())"
	})
	@SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Integer.class)
	int insert(ArticleRelationship record);
	
	@Select({
		"select id, site_id, type, cn_id, jp_id, en_id, kr_id, th_id, tw_id, create_at from `relationship`",
		"where id = #{id, jdbcType=INTEGER}"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
	    @Result(column="type", property="type", jdbcType=JdbcType.VARCHAR),
	    @Result(column="cn_id", property="cnId", jdbcType=JdbcType.INTEGER),
	    @Result(column="jp_id", property="jpId", jdbcType=JdbcType.INTEGER),
	    @Result(column="en_id", property="enId", jdbcType=JdbcType.INTEGER),
	    @Result(column="kr_id", property="krId", jdbcType=JdbcType.INTEGER),
	    @Result(column="th_id", property="thId", jdbcType=JdbcType.INTEGER),
	    @Result(column="tw_id", property="twId", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP)
	})
	ArticleRelationship selectById(@Param("id")int id);
	
	@SelectProvider(type = ArticleRelationshipMapperProvider.class, method = "selectBySiteIdAndType")
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
	    @Result(column="type", property="type", jdbcType=JdbcType.VARCHAR),
	    @Result(column="cn_id", property="cnId", jdbcType=JdbcType.INTEGER),
	    @Result(column="jp_id", property="jpId", jdbcType=JdbcType.INTEGER),
	    @Result(column="en_id", property="enId", jdbcType=JdbcType.INTEGER),
	    @Result(column="kr_id", property="krId", jdbcType=JdbcType.INTEGER),
	    @Result(column="th_id", property="thId", jdbcType=JdbcType.INTEGER),
	    @Result(column="tw_id", property="twId", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP)
	})
	List<ArticleRelationship> selectByCnIds(@Param("cnIds")List<Integer> cndIds);
	
	@Select({
		"select id, site_id, type, cn_id, jp_id, en_id, kr_id, th_id, tw_id, create_at from `relationship`",
		"where site_id = #{siteId, jdbcType=INTEGER} and type = #{type, jdbcType=VARCHAR}"
	})
	@Results({
		@Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
	    @Result(column="type", property="type", jdbcType=JdbcType.VARCHAR),
	    @Result(column="cn_id", property="cnId", jdbcType=JdbcType.INTEGER),
	    @Result(column="jp_id", property="jpId", jdbcType=JdbcType.INTEGER),
	    @Result(column="en_id", property="enId", jdbcType=JdbcType.INTEGER),
	    @Result(column="kr_id", property="krId", jdbcType=JdbcType.INTEGER),
	    @Result(column="th_id", property="thId", jdbcType=JdbcType.INTEGER),
	    @Result(column="tw_id", property="twId", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP)
	})
	List<ArticleRelationship> selectBySiteIdAndType(@Param("siteId")int siteId, @Param("type")String type);
	
	@Select({
		"select id, site_id, type, cn_id, jp_id, en_id, kr_id, th_id, tw_id, create_at from `relationship`",
		"where site_id = #{siteId, jdbcType=INTEGER}"
	})
	@Results({
		@Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
	    @Result(column="type", property="type", jdbcType=JdbcType.VARCHAR),
	    @Result(column="cn_id", property="cnId", jdbcType=JdbcType.INTEGER),
	    @Result(column="jp_id", property="jpId", jdbcType=JdbcType.INTEGER),
	    @Result(column="en_id", property="enId", jdbcType=JdbcType.INTEGER),
	    @Result(column="kr_id", property="krId", jdbcType=JdbcType.INTEGER),
	    @Result(column="th_id", property="thId", jdbcType=JdbcType.INTEGER),
	    @Result(column="tw_id", property="twId", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP)
	})
	List<ArticleRelationship> selectBySiteId(@Param("siteId")int siteId);
	
	
	@SelectProvider(type = ArticleRelationshipMapperProvider.class, method = "selectBySiteIdAndType")
	@Results({
		@Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
	    @Result(column="type", property="type", jdbcType=JdbcType.VARCHAR),
	    @Result(column="cn_id", property="cnId", jdbcType=JdbcType.INTEGER),
	    @Result(column="jp_id", property="jpId", jdbcType=JdbcType.INTEGER),
	    @Result(column="en_id", property="enId", jdbcType=JdbcType.INTEGER),
	    @Result(column="kr_id", property="krId", jdbcType=JdbcType.INTEGER),
	    @Result(column="th_id", property="thId", jdbcType=JdbcType.INTEGER),
	    @Result(column="tw_id", property="twId", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP)
	})
	List<ArticleRelationship> selectBySiteIdAndTypes(@Param("siteId")int siteId, @Param("types")List<String> types);
	
	@Update({
	   "update `relationship`",
	   	"set type = #{type,jdbcType=VARCHAR},",
		   	"cn_id = #{cnId,jdbcType=INTEGER},",
		   	"jp_id = #{jpId,jdbcType=INTEGER},",
		   	"en_id = #{enId,jdbcType=INTEGER},",
		   	"kr_id = #{krId,jdbcType=INTEGER},",
		   	"th_id = #{thId,jdbcType=INTEGER},",
		   	"tw_id = #{twId,jdbcType=INTEGER}",
	   "where Id = #{id,jdbcType=INTEGER}"
	})
	int updateByPrimaryKey(ArticleRelationship record);
}
